﻿#include "user_main.h"
#include "user_recv_transmit.h"
#include "user_transmit_channel_def.h"
#include "Usart.h"
#include "gt1151q.h"

#define TRANSMIT_USART USART1
int __io_putchar(int ch){
    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)&ch,1);
    return ch;
}
int __io_getchar(void){
    uint8_t ch;
    yz_uart_receive(TRANSMIT_USART,&ch,1);
    return ch;
}
static const char *initP1="传输协议初始化成功.\n";
void screen_init(void){
    /* 设置背光控制引脚PD12 推挽输出 */
    gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_12);
    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);

    /* 设置LCD复位引脚PD13 推挽输出 */
    gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_13);
    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);

    tli_config();
}
YZF_BOOL file_init(void){
    FRESULT res_sd= f_mount(&yzl_fs,"/MMC/",1);
    if (res_sd == FR_NO_FILESYSTEM) {
        yz_info("》SD卡还没有文件系统，即将进行格式化...\n");
        res_sd=f_mkfs("/MMC/",0,0,0);
        if (res_sd == FR_OK) {
            yz_info("》SD卡已成功格式化文件系统。\n");
            res_sd = f_mount(NULL,"/MMC/",1);
            res_sd = f_mount(&yzl_fs,"/MMC/",1);
            return YZF_TRUE;
        } else {
            yz_info("《《格式化失败。》》\n");
            return YZF_FALSE;
        }
    } else if (res_sd!=FR_OK) {
        yz_info("！！SD卡挂载文件系统失败。(%d)\n",res_sd);
        yz_info("！！可能原因：SD卡初始化不成功。\n");
        return YZF_FALSE;
    }else{
        return YZF_TRUE;
    }
}
YZ_MemManger freeRTOSMem;
YZ_MemManger yzlMem;
void sram_manager_init(){
    /** FreeRTOS Part*/
    freeRTOSMem.pFree=vPortFree;
    freeRTOSMem.pMalloc=pvPortMalloc;
    freeRTOSMem.baseAddress=YZ_SDRAM_FreeRTOS_MANAGER_RAM_BASE;
    freeRTOSMem.size=YZ_SDRAM_FreeRTOS_MANAGER_RAM;

    /** YzMemManger Part*/
    yzlMem.baseAddress=YZ_SDRAM_USER_DATA_LOAD_BASE;
    yzlMem.size=YZ_SDRAM_USER_DATA_LOAD;
    yzl_mem_init(&yzlMem);
}
int user_main(void){
    user_recv_transmit_init();

    yzl_clock_init();

    yz_info("+++++++++ System Load +++++++++\n");

    sram_manager_init();
    yz_info("内存管理初始化成功\n");

    if(file_init()){
        yz_info("文件系统加载成功\n");
    }else{
        yz_info("文件系统加载失败\n");
        return -1;
    }

    yzl_font_init("FontBin16");
    yz_info("字体系统加载成功\n");

    screen_init();
    yz_info("屏幕初始化成功\n");

    yzl_ui_init(WHITE);
    yz_info("UI系统初始化成功\n");

//    yzl_gt911_screen_touch_init((YZL_GPIO){GPIOB,GPIO_PIN_13},(YZL_GPIO){GPIOB,GPIO_PIN_15},(YZL_GPIO){GPIOB,GPIO_PIN_12},(YZL_GPIO){GPIOD,GPIO_PIN_11});
    gt1151_init();

    user_recv_transmit_channel_init();
    yz_info(initP1);
    //用户初始化函数编写
    return 0;
}
YZF_BOOL OutError=YZF_FALSE;
//跳出错误函数
void user_OutError(){
    OutError=YZF_TRUE;
}

void user_error_handler(YZF_RESULT error){
    switch (error) {
        case YZF_NOT_FOUND:
            yz_error("[GlobalSheet: %hd] 参数未找到.\n",yz_status_sheet_get(YZ_FLAG_ERROR_HANDLER_INDEX));
            break;
        case YZF_ERROR:
            yz_error("[GlobalSheet: %hd] 未知错误.\n",yz_status_sheet_get(YZ_FLAG_ERROR_HANDLER_INDEX));
            break;
        case YZF_NOT_INIT:
            yz_error("[GlobalSheet: %hd] 使用的函数，所需对象未被初始化.\n",yz_status_sheet_get(YZ_FLAG_ERROR_HANDLER_INDEX));
            break;
        case YZF_CRC_ERROR:
            yz_error("[GlobalSheet: %hd] CRC校验错误.\n",yz_status_sheet_get(YZ_FLAG_ERROR_HANDLER_INDEX));
            break;
        case YZF_CLOCK_NOT_READY:
            yz_error("[GlobalSheet: %hd] 时钟未就绪.\n",yz_status_sheet_get(YZ_FLAG_ERROR_HANDLER_INDEX));
            break;
        case YZF_NOTHING:
#ifdef osCMSIS
            osDelay(1);
#endif
#ifdef GD32F470
            vTaskDelay(1);
#endif
            user_OutError();
            break;
        default:
            yz_error("[GlobalSheet: %hd] 未被识别的错误类型. Code: %d\n",yz_status_sheet_get(YZ_FLAG_ERROR_HANDLER_INDEX),error);
    }
    while (!OutError){
        __asm__("nop");
    }
    OutError=YZF_FALSE;
}

void yz_error(const char *fmt, ...){
    char *str_error="[ERROR]";
    int i;

    va_list arg;
    va_start(arg, fmt);
    memset(transmit_buf,0,sizeof(transmit_buf));
    i = vsprintf(transmit_buf, fmt, arg);
    va_end(arg);

    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)str_error, strlen(str_error));
    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)transmit_buf,i);
//    if(Yz_FontInitFlag){
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,(YZ_COLOR)RED,(unsigned char *)str_error);
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,(YZ_COLOR)RED,(unsigned char *)transmit_buf);
//    }
}
void yz_info(const char *fmt, ...){
    char *str_error="[INFO]";
    int i;

    va_list arg;
    va_start(arg, fmt);
    memset(transmit_buf,0,sizeof(transmit_buf));
    i = vsprintf(transmit_buf, fmt, arg);
    va_end(arg);

    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)str_error, strlen(str_error));
    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)transmit_buf,i);
//    if(Yz_FontInitFlag){
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,(YZ_COLOR)WHITE,(unsigned char *)str_error);
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,(YZ_COLOR)WHITE,(unsigned char *)transmit_buf);
//    }
}
void yz_debug(const char *fmt, ...){
    char *str_error="[DEBUG]";
    int i;

    va_list arg;
    va_start(arg, fmt);
    memset(transmit_buf,0,sizeof(transmit_buf));
    i = vsprintf(transmit_buf, fmt, arg);
    va_end(arg);

    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)str_error, strlen(str_error));
    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)transmit_buf,i);
//    if(Yz_FontInitFlag){
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,(YZ_COLOR)YELLOW,(unsigned char *)str_error);
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,(YZ_COLOR)YELLOW,(unsigned char *)transmit_buf);
//    }
}
void yz_ok(const char *fmt, ...){
    char *str_error="[SUCCESS]";
    int i;

    va_list arg;
    va_start(arg, fmt);
    memset(transmit_buf,0,sizeof(transmit_buf));
    i = vsprintf(transmit_buf, fmt, arg);
    va_end(arg);

    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)str_error, strlen(str_error));
    yz_uart_transmit(TRANSMIT_USART,(uint8_t *)transmit_buf,i);
//    if(Yz_FontInitFlag){
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,GREEN,(unsigned char *)str_error);
//        USER_SCREEN_WRITE_TEXT_UTF8((YZ_COLOR)BLACK,GREEN,(unsigned char *)transmit_buf);
//    }
}